package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.common.model.hosp.Hospital;
import com.atguigu.yygh.common.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    public void save(Map<String, Object> requestMap) {
        //把参数map集合转换成对象 Hospital
        String mapString = JSONObject.toJSONString(requestMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断数据是否存在
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist =  hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在,则更新
        if (hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(hospitalExist.getUpdateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else { //如果不存在,则添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 分页显示医院列表
     *
     * @param page            当前页
     * @param limit           总页数
     * @param hospitalQueryVo 参数实体类
     * @return 分页后的数据
     */
    @Override
    public Page<Hospital> findPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建分页对象
        Pageable pageable = PageRequest.of(page-1,limit);

        //创建条件构造器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //复制对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //4. 创建Example
        Example<Hospital> example = Example.of(hospital,matcher);

        //5.调用方法
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //遍历list集合，查到每一个医院，封装医院等级
        pages.getContent().stream().forEach(item ->{
            this.setHospitalHosType(item);
        });
        return pages;
    }


    private Hospital setHospitalHosType(Hospital hospital) {

        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市 地区
        String provinceCodeString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityCodeString = dictFeignClient.getName(hospital.getCityCode());
        String districtCodeString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceCodeString+cityCodeString+districtCodeString);
        return hospital;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询出医院
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置前端传过来的状态
        hospital.setStatus(status);
        //更新日期
        hospital.setUpdateTime(new Date());
        //执行更新
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospById(String id) {

        Map<String,Object> map = new HashMap<>(16);

        //医院信息(包含等级以及地址)
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        //封装医院信息
        map.put("hospital",hospital);
        //封装预约信息
        map.put("bookingRule",hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (null!=hospital){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByName(String hosname) {
       List<Hospital> hospitalList = hospitalRepository.findHospitalByHosnameLike(hosname);
        return hospitalList;
    }

    @Override
    public Map<String, Object> hospDetail(String hoscode) {
        Map<String,Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }
}
